#include "MySet.h"
#include <algorithm>

MySet::MySet() : MyVector() {}

bool MySet::IsElement(const std::string &element) const
{
    return find(element) != -1;
}

void MySet::AddElement(const std::string &element)
{
    if (!IsElement(element))
    {
        add_element(element);
    }
}

void MySet::DeleteElement(const std::string &element)
{
    int index = find(element);
    if (index != -1)
    {
        delete_element(index);
    }
}

MySet &MySet::operator+=(const MySet &other)
{
    for (int i = 0; i < other.size; ++i)
    {
        AddElement(other.pdata[i]);
    }
    return *this;
}

MySet &MySet::operator-=(const MySet &other)
{
    for (int i = 0; i < other.size; ++i)
    {
        DeleteElement(other.pdata[i]);
    }
    return *this;
}

MySet &MySet::operator*=(const MySet &other)
{
    MySet intersection;
    for (int i = 0; i < size; ++i)
    {
        if (other.IsElement(pdata[i]))
        {
            intersection.AddElement(pdata[i]);
        }
    }
    *this = intersection;
    return *this;
}

std::ostream &operator<<(std::ostream &os, const MySet &set)
{
    os << "{";
    for (int i = 0; i < set.size; ++i)
    {
        os << set.pdata[i];
        if (i < set.size - 1)
        {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

MySet operator+(const MySet &set1, const MySet &set2)
{
    MySet result = set1;
    result += set2;

    // Сортировка элементов в результирующем множестве
    std::sort(result.pdata, result.pdata + result.size);

    return result;
}

MySet operator-(const MySet &set1, const MySet &set2)
{
    MySet result = set1;
    result -= set2;
    return result;
}

MySet operator*(const MySet &set1, const MySet &set2)
{
    MySet result = set1;
    result *= set2;
    return result;
}

bool operator==(const MySet &set1, const MySet &set2)
{
    if (set1.size != set2.size)
    {
        return false;
    }
    for (int i = 0; i < set1.size; ++i)
    {
        if (!set2.IsElement(set1.pdata[i]))
        {
            return false;
        }
    }
    return true;
}
